

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Fly542">
  <meta name="keywords" content="">
  
    <meta name="description" content="本文转自 https:&#x2F;&#x2F;zhuanlan.zhihu.com&#x2F;p&#x2F;335994370 1. 什么是左值、右值首先不考虑引用以减少干扰，可以从2个角度判断：左值可以取地址、位于等号左边；而右值没法取地址，位于等号右边。 1int a &#x3D; 5;   a可以通过 &amp; 取地址，位于等号左边，所以a是左值。 5位于等号右边，5没法通过 &amp; 取地址，所以5是个右值。  再举个例子： 1234">
<meta property="og:type" content="article">
<meta property="og:title" content="一文读懂C++右值引用和std::move(转载)">
<meta property="og:url" content="http://fly542.cn/2023/11/02/03%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/01c_cpp/17%E4%B8%80%E6%96%87%E8%AF%BB%E6%87%82C++%E5%8F%B3%E5%80%BC%E5%BC%95%E7%94%A8(%E8%BD%AC%E8%BD%BD)/index.html">
<meta property="og:site_name" content="Fly542 技术沉淀">
<meta property="og:description" content="本文转自 https:&#x2F;&#x2F;zhuanlan.zhihu.com&#x2F;p&#x2F;335994370 1. 什么是左值、右值首先不考虑引用以减少干扰，可以从2个角度判断：左值可以取地址、位于等号左边；而右值没法取地址，位于等号右边。 1int a &#x3D; 5;   a可以通过 &amp; 取地址，位于等号左边，所以a是左值。 5位于等号右边，5没法通过 &amp; 取地址，所以5是个右值。  再举个例子： 1234">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-11-02T09:35:00.000Z">
<meta property="article:modified_time" content="2023-11-02T09:44:47.852Z">
<meta property="article:author" content="Fly542">
<meta property="article:tag" content="c++">
<meta property="article:tag" content="c11">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>一文读懂C++右值引用和std::move(转载) - Fly542 技术沉淀</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"fly542.cn","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fly542 技术沉淀</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="一文读懂C++右值引用和std::move(转载)">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2023-11-02 17:35" pubdate>
        2023年11月2日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      7.8k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      65 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">一文读懂C++右值引用和std::move(转载)</h1>
            
            <div class="markdown-body">
              <p>本文转自 <a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/335994370">https://zhuanlan.zhihu.com/p/335994370</a></p>
<h1 id="1-什么是左值、右值"><a href="#1-什么是左值、右值" class="headerlink" title="1. 什么是左值、右值"></a><strong>1. 什么是左值、右值</strong></h1><p>首先不考虑引用以减少干扰，可以从2个角度判断：左值<strong>可以取地址、位于等号左边</strong>；而右值<strong>没法取地址，位于等号右边</strong>。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;<br></code></pre></td></tr></table></figure>

<ul>
<li>a可以通过 &amp; 取地址，位于等号左边，所以a是左值。</li>
<li>5位于等号右边，5没法通过 &amp; 取地址，所以5是个右值。</li>
</ul>
<p>再举个例子：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">A</span> &#123;</span><br>    <span class="hljs-built_in">A</span>(<span class="hljs-keyword">int</span> a = <span class="hljs-number">0</span>) &#123;<br>        a_ = a;<br>    &#125;<br>    <span class="hljs-keyword">int</span> a_;<br>&#125;;<br>A a = <span class="hljs-built_in">A</span>();<br></code></pre></td></tr></table></figure>

<ul>
<li>同样的，a可以通过 &amp; 取地址，位于等号左边，所以a是左值。</li>
<li>A()是个临时值，没法通过 &amp; 取地址，位于等号右边，所以A()是个右值。</li>
</ul>
<p>可见左右值的概念很清晰，<font color=#ff0000>有地址的变量就是左值，没有地址的字面值、临时值就是右值。</font></p>
<h1 id="2-什么是左值引用、右值引用"><a href="#2-什么是左值引用、右值引用" class="headerlink" title="2. 什么是左值引用、右值引用"></a><strong>2. 什么是左值引用、右值引用</strong></h1><p><strong>引用本质是别名，可以通过引用修改变量的值，传参时传引用可以避免拷贝，其实现原理和指针类似。</strong></p>
<p>个人认为，引用出现的本意是为了降低C语言指针的使用难度，但现在指针+左右值引用共同存在，反而大大增加了学习和理解成本。</p>
<h2 id="2-1-左值引用"><a href="#2-1-左值引用" class="headerlink" title="2.1 左值引用"></a><strong>2.1 左值引用</strong></h2><p>左值引用大家都很熟悉，<strong>能指向左值，不能指向右值的就是左值引用</strong>：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;<br><span class="hljs-keyword">int</span> &amp;ref_a = a; <span class="hljs-comment">// 左值引用指向左值，编译通过</span><br><span class="hljs-keyword">int</span> &amp;ref_a = <span class="hljs-number">5</span>; <span class="hljs-comment">// 左值引用指向了右值，会编译失败</span><br></code></pre></td></tr></table></figure>

<p><strong>引用是变量的别名，由于右值没有地址，没法被修改，所以左值引用无法指向右值。</strong></p>
<p>但是，const左值引用是可以指向右值的：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> &amp;ref_a = <span class="hljs-number">5</span>;  <span class="hljs-comment">// 编译通过</span><br></code></pre></td></tr></table></figure>

<p>const 左值引用不会修改指向值，因此可以指向右值，这也是为什么要使用<code>const &amp;</code>作为函数参数的原因之一，如<code>std::vector</code>的<code>push_back</code>：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">push_back</span> <span class="hljs-params">(<span class="hljs-keyword">const</span> value_type&amp; val)</span></span>;<br></code></pre></td></tr></table></figure>

<p>如果没有<code>const</code>，<code>vec.push_back(5)</code>这样的代码就无法编译通过了。</p>
<h2 id="2-2-右值引用"><a href="#2-2-右值引用" class="headerlink" title="2.2 右值引用"></a><strong>2.2 右值引用</strong></h2><p>再看下右值引用，右值引用的标志是<code>&amp;&amp;</code>，顾名思义，右值引用专门为右值而生，<strong>可以指向右值，不能指向左值</strong>：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">int</span> &amp;&amp;ref_a_right = <span class="hljs-number">5</span>; <span class="hljs-comment">// ok</span><br> <br><span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;<br><span class="hljs-keyword">int</span> &amp;&amp;ref_a_left = a; <span class="hljs-comment">// 编译不过，右值引用不可以指向左值</span><br> <br>ref_a_right = <span class="hljs-number">6</span>; <span class="hljs-comment">// 右值引用的用途：可以修改右值</span><br></code></pre></td></tr></table></figure>

<h2 id="2-3-对左右值引用本质的讨论"><a href="#2-3-对左右值引用本质的讨论" class="headerlink" title="2.3 对左右值引用本质的讨论"></a><strong>2.3 对左右值引用本质的讨论</strong></h2><p>下边的论述比较复杂，也是本文的核心，对理解这些概念非常重要。</p>
<h3 id="2-3-1-右值引用有办法指向左值吗？"><a href="#2-3-1-右值引用有办法指向左值吗？" class="headerlink" title="2.3.1 右值引用有办法指向左值吗？"></a><strong>2.3.1 右值引用有办法指向左值吗？</strong></h3><p>有办法，<code>std::move</code>：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>; <span class="hljs-comment">// a是个左值</span><br><span class="hljs-keyword">int</span> &amp;ref_a_left = a; <span class="hljs-comment">// 左值引用指向左值</span><br><span class="hljs-keyword">int</span> &amp;&amp;ref_a_right = std::<span class="hljs-built_in">move</span>(a); <span class="hljs-comment">// 通过std::move将左值转化为右值，可以被右值引用指向</span><br> <br>cout &lt;&lt; a; <span class="hljs-comment">// 打印结果：5</span><br></code></pre></td></tr></table></figure>

<p>在上边的代码里，看上去是左值a通过std::move移动到了右值ref_a_right中，那是不是a里边就没有值了？并不是，打印出a的值仍然是5。</p>
<p><code>std::move</code>是一个非常有迷惑性的函数，不理解左右值概念的人们往往以为它能把一个变量里的内容移动到另一个变量，<strong>但事实上std::move移动不了什么，唯一的功能是把左值强制转化为右值</strong>，让右值引用可以指向左值。其实现等同于一个类型转换：<code>static_cast&lt;T&amp;&amp;&gt;(lvalue)</code>。 所以，<strong>单纯的std::move(xxx)不会有性能提升</strong>，std::move的使用场景在第三章会讲。</p>
<p>同样的，右值引用能指向右值，本质上也是把右值提升为一个左值，并定义一个右值引用通过std::move指向该左值：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">int</span> &amp;&amp;ref_a = <span class="hljs-number">5</span>;<br>ref_a = <span class="hljs-number">6</span>; <br> <br>等同于以下代码：<br> <br><span class="hljs-keyword">int</span> temp = <span class="hljs-number">5</span>;<br><span class="hljs-keyword">int</span> &amp;&amp;ref_a = std::<span class="hljs-built_in">move</span>(temp);<br>ref_a = <span class="hljs-number">6</span>;<br></code></pre></td></tr></table></figure>

<h3 id="2-3-2-左值引用、右值引用本身是左值还是右值？"><a href="#2-3-2-左值引用、右值引用本身是左值还是右值？" class="headerlink" title="2.3.2 左值引用、右值引用本身是左值还是右值？"></a><strong>2.3.2 左值引用、右值引用本身是左值还是右值？</strong></h3><p><strong>被声明出来的左、右值引用都是左值</strong>。 因为被声明出的左右值引用是有地址的，也位于等号左边。仔细看下边代码：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-comment">// 形参是个右值引用</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">change</span><span class="hljs-params">(<span class="hljs-keyword">int</span>&amp;&amp; right_value)</span> </span>&#123;<br>    right_value = <span class="hljs-number">8</span>;<br>&#125;<br> <br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>; <span class="hljs-comment">// a是个左值</span><br>    <span class="hljs-keyword">int</span> &amp;ref_a_left = a; <span class="hljs-comment">// ref_a_left是个左值引用</span><br>    <span class="hljs-keyword">int</span> &amp;&amp;ref_a_right = std::<span class="hljs-built_in">move</span>(a); <span class="hljs-comment">// ref_a_right是个右值引用</span><br> <br>    <span class="hljs-built_in">change</span>(a); <span class="hljs-comment">// 编译不过，a是左值，change参数要求右值</span><br>    <span class="hljs-built_in">change</span>(ref_a_left); <span class="hljs-comment">// 编译不过，左值引用ref_a_left本身也是个左值</span><br>    <span class="hljs-built_in">change</span>(ref_a_right); <span class="hljs-comment">// 编译不过，右值引用ref_a_right本身也是个左值</span><br>     <br>    <span class="hljs-built_in">change</span>(std::<span class="hljs-built_in">move</span>(a)); <span class="hljs-comment">// 编译通过</span><br>    <span class="hljs-built_in">change</span>(std::<span class="hljs-built_in">move</span>(ref_a_right)); <span class="hljs-comment">// 编译通过</span><br>    <span class="hljs-built_in">change</span>(std::<span class="hljs-built_in">move</span>(ref_a_left)); <span class="hljs-comment">// 编译通过</span><br> <br>    <span class="hljs-built_in">change</span>(<span class="hljs-number">5</span>); <span class="hljs-comment">// 当然可以直接接右值，编译通过</span><br>     <br>    cout &lt;&lt; &amp;a &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span>;<br>    cout &lt;&lt; &amp;ref_a_left &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span>;<br>    cout &lt;&lt; &amp;ref_a_right;<br>    <span class="hljs-comment">// 打印这三个左值的地址，都是一样的</span><br>&#125;<br></code></pre></td></tr></table></figure>

<p>看完后你可能有个问题，std::move会返回一个右值引用<code>int &amp;&amp;</code>，它是左值还是右值呢？ 从表达式<code>int &amp;&amp;ref = std::move(a)</code>来看，右值引用<code>ref</code>指向的必须是右值，所以move返回的<code>int &amp;&amp;</code>是个右值。所以右值引用既可能是左值，又可能是右值吗？ 确实如此：<strong>右值引用既可以是左值也可以是右值，如果有名称则为左值，否则是右值</strong>。</p>
<p>或者说：<strong>作为函数返回值的 &amp;&amp; 是右值，直接声明出来的 &amp;&amp; 是左值</strong>。 这同样也符合第一章对左值，右值的判定方式：其实引用和普通变量是一样的，<code>int &amp;&amp;ref = std::move(a)</code>和 <code>int a = 5</code>没有什么区别，等号左边就是左值，右边就是右值。</p>
<p>最后，从上述分析中我们得到如下结论：</p>
<ol>
<li><strong>从性能上讲，左右值引用没有区别，传参使用左右值引用都可以避免拷贝。</strong></li>
<li><strong>右值引用可以直接指向右值，也可以通过std::move指向左值；而左值引用只能指向左值(const左值引用也能指向右值)。</strong></li>
<li><strong>作为函数形参时，右值引用更灵活。虽然const左值引用也可以做到左右值都接受，但它无法修改，有一定局限性。</strong></li>
</ol>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">f</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span>&amp; n)</span> </span>&#123;<br>    n += <span class="hljs-number">1</span>; <span class="hljs-comment">// 编译失败，const左值引用不能修改指向变量</span><br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">f2</span><span class="hljs-params">(<span class="hljs-keyword">int</span> &amp;&amp; n)</span> </span>&#123;<br>    n += <span class="hljs-number">1</span>; <span class="hljs-comment">// ok</span><br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-built_in">f</span>(<span class="hljs-number">5</span>);<br>    <span class="hljs-built_in">f2</span>(<span class="hljs-number">5</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="3-右值引用和std-move的应用场景"><a href="#3-右值引用和std-move的应用场景" class="headerlink" title="3. 右值引用和std::move的应用场景"></a><strong>3. 右值引用和std::move的应用场景</strong></h1><p>按上文分析，<code>std::move</code>只是类型转换工具，不会对性能有好处；右值引用在作为函数形参时更具灵活性，看上去还是挺鸡肋的。他们有什么实际应用场景吗？</p>
<h2 id="3-1-实现移动语义"><a href="#3-1-实现移动语义" class="headerlink" title="3.1 实现移动语义"></a><strong>3.1 实现移动语义</strong></h2><p>在实际场景中，右值引用和std::move被广泛用于在STL和自定义类中<strong>实现移动语义，避免拷贝，从而提升程序性能</strong>。 在没有右值引用之前，一个简单的数组类通常实现如下，有<code>构造函数</code>、<code>拷贝构造函数</code>、<code>赋值运算符重载</code>、<code>析构函数</code>等。深拷贝/浅拷贝在此不做讲解。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Array</span> &#123;</span><br><span class="hljs-keyword">public</span>:<br>    <span class="hljs-built_in">Array</span>(<span class="hljs-keyword">int</span> size) : <span class="hljs-built_in">size_</span>(size) &#123;<br>        data = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[size_];<br>    &#125;<br>     <br>    <span class="hljs-comment">// 深拷贝构造</span><br>    <span class="hljs-built_in">Array</span>(<span class="hljs-keyword">const</span> Array&amp; temp_array) &#123;<br>        size_ = temp_array.size_;<br>        data_ = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[size_];<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; size_; i ++) &#123;<br>            data_[i] = temp_array.data_[i];<br>        &#125;<br>    &#125;<br>     <br>    <span class="hljs-comment">// 深拷贝赋值</span><br>    Array&amp; <span class="hljs-keyword">operator</span>=(<span class="hljs-keyword">const</span> Array&amp; temp_array) &#123;<br>        <span class="hljs-keyword">delete</span>[] data_;<br> <br>        size_ = temp_array.size_;<br>        data_ = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[size_];<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; size_; i ++) &#123;<br>            data_[i] = temp_array.data_[i];<br>        &#125;<br>    &#125;<br> <br>    ~<span class="hljs-built_in">Array</span>() &#123;<br>        <span class="hljs-keyword">delete</span>[] data_;<br>    &#125;<br> <br><span class="hljs-keyword">public</span>:<br>    <span class="hljs-keyword">int</span> *data_;<br>    <span class="hljs-keyword">int</span> size_;<br>&#125;;<br></code></pre></td></tr></table></figure>

<p>该类的拷贝构造函数、赋值运算符重载函数已经通过使用左值引用传参来避免一次多余拷贝了，但是内部实现要深拷贝，无法避免。 这时，有人提出一个想法：是不是可以提供一个<code>移动构造函数</code>，把被拷贝者的数据移动过来，被拷贝者后边就不要了，这样就可以避免深拷贝了，如：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Array</span> &#123;</span><br><span class="hljs-keyword">public</span>:<br>    <span class="hljs-built_in">Array</span>(<span class="hljs-keyword">int</span> size) : <span class="hljs-built_in">size_</span>(size) &#123;<br>        data = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[size_];<br>    &#125;<br>     <br>    <span class="hljs-comment">// 深拷贝构造</span><br>    <span class="hljs-built_in">Array</span>(<span class="hljs-keyword">const</span> Array&amp; temp_array) &#123;<br>        ...<br>    &#125;<br>     <br>    <span class="hljs-comment">// 深拷贝赋值</span><br>    Array&amp; <span class="hljs-keyword">operator</span>=(<span class="hljs-keyword">const</span> Array&amp; temp_array) &#123;<br>        ...<br>    &#125;<br> <br>    <span class="hljs-comment">// 移动构造函数，可以浅拷贝</span><br>    <span class="hljs-built_in">Array</span>(<span class="hljs-keyword">const</span> Array&amp; temp_array, <span class="hljs-keyword">bool</span> move) &#123;<br>        data_ = temp_array.data_;<br>        size_ = temp_array.size_;<br>        <span class="hljs-comment">// 为防止temp_array析构时delete data，提前置空其data_      </span><br>        temp_array.data_ = <span class="hljs-literal">nullptr</span>;<br>    &#125;<br>     <br> <br>    ~<span class="hljs-built_in">Array</span>() &#123;<br>        <span class="hljs-keyword">delete</span> [] data_;<br>    &#125;<br> <br><span class="hljs-keyword">public</span>:<br>    <span class="hljs-keyword">int</span> *data_;<br>    <span class="hljs-keyword">int</span> size_;<br>&#125;;<br></code></pre></td></tr></table></figure>

<p>这么做有2个问题：</p>
<ul>
<li>不优雅，表示移动语义还需要一个额外的参数(或者其他方式)。</li>
<li>无法实现！<code>temp_array</code>是个const左值引用，无法被修改，所以<code>temp_array.data_ = nullptr;</code>这行会编译不过。当然函数参数可以改成非const：<code>Array(Array&amp; temp_array, bool move)&#123;...&#125;</code>，这样也有问题，由于左值引用不能接右值，<code>Array a = Array(Array(), true);</code>这种调用方式就没法用了。</li>
</ul>
<p>可以发现左值引用真是用的很不爽，<strong>右值引用的出现解决了这个问题</strong>，在STL的很多容器中，都实现了以<strong>右值引用为参数</strong>的<code>移动构造函数</code>和<code>移动赋值重载函数</code>，或者其他函数，最常见的如std::vector的<code>push_back</code>和<code>emplace_back</code>。参数为左值引用意味着拷贝，为右值引用意味着移动。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Array</span> &#123;</span><br><span class="hljs-keyword">public</span>:<br>    ......<br> <br>    <span class="hljs-comment">// 优雅</span><br>    <span class="hljs-built_in">Array</span>(Array&amp;&amp; temp_array) &#123;<br>        data_ = temp_array.data_;<br>        size_ = temp_array.size_;<br>        <span class="hljs-comment">// 为防止temp_array析构时delete data，提前置空其data_      </span><br>        temp_array.data_ = <span class="hljs-literal">nullptr</span>;<br>    &#125;<br>     <br> <br><span class="hljs-keyword">public</span>:<br>    <span class="hljs-keyword">int</span> *data_;<br>    <span class="hljs-keyword">int</span> size_;<br>&#125;;<br></code></pre></td></tr></table></figure>

<p>如何使用：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-comment">// 例1：Array用法</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>&#123;<br>    Array a;<br> <br>    <span class="hljs-comment">// 做一些操作</span><br>    .....<br>     <br>    <span class="hljs-comment">// 左值a，用std::move转化为右值</span><br>    <span class="hljs-function">Array <span class="hljs-title">b</span><span class="hljs-params">(std::move(a))</span></span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="3-2-实例：vector-push-back使用std-move提高性能"><a href="#3-2-实例：vector-push-back使用std-move提高性能" class="headerlink" title="3.2 实例：vector::push_back使用std::move提高性能"></a><strong>3.2 实例：vector::push_back使用std::move提高性能</strong></h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-comment">// 例2：std::vector和std::string的实际例子</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    std::string str1 = <span class="hljs-string">&quot;aacasxs&quot;</span>;<br>    std::vector&lt;std::string&gt; vec;<br>     <br>    vec.<span class="hljs-built_in">push_back</span>(str1); <span class="hljs-comment">// 传统方法，copy</span><br>    vec.<span class="hljs-built_in">push_back</span>(std::<span class="hljs-built_in">move</span>(str1)); <span class="hljs-comment">// 调用移动语义的push_back方法，避免拷贝，str1会失去原有值，变成空字符串</span><br>    vec.<span class="hljs-built_in">emplace_back</span>(std::<span class="hljs-built_in">move</span>(str1)); <span class="hljs-comment">// emplace_back效果相同，str1会失去原有值</span><br>    vec.<span class="hljs-built_in">emplace_back</span>(<span class="hljs-string">&quot;axcsddcas&quot;</span>); <span class="hljs-comment">// 当然可以直接接右值</span><br>&#125;<br> <br><span class="hljs-comment">// std::vector方法定义</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">push_back</span> <span class="hljs-params">(<span class="hljs-keyword">const</span> value_type&amp; val)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">push_back</span> <span class="hljs-params">(value_type&amp;&amp; val)</span></span>;<br> <br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">emplace_back</span> <span class="hljs-params">(Args&amp;&amp;... args)</span></span>;<br></code></pre></td></tr></table></figure>

<p>在vector和string这个场景，加个<code>std::move</code>会调用到移动语义函数，避免了深拷贝。</p>
<p>除非设计不允许移动，STL类大都支持移动语义函数，即<code>可移动的</code>。 另外，编译器会<strong>默认</strong>在用户自定义的<code>class</code>和<code>struct</code>中生成移动语义函数，但前提是用户没有主动定义该类的<code>拷贝构造</code>等函数(具体规则自行百度哈)。 <strong>因此，可移动对象在&lt;需要拷贝且被拷贝者之后不再被需要&gt;的场景，建议使用</strong><code>std::move</code><strong>触发移动语义，提升性能。</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs cpp">moveable_objecta = moveable_objectb; <br>改为： <br>moveable_objecta = std::<span class="hljs-built_in">move</span>(moveable_objectb);<br></code></pre></td></tr></table></figure>

<p>还有些STL类是<code>move-only</code>的，比如<code>unique_ptr</code>，这种类只有移动构造函数，因此只能移动(转移内部对象所有权，或者叫浅拷贝)，不能拷贝(深拷贝):</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs cpp">std::unique_ptr&lt;A&gt; ptr_a = std::make_unique&lt;A&gt;();<br><br>std::unique_ptr&lt;A&gt; ptr_b = std::<span class="hljs-built_in">move</span>(ptr_a); <span class="hljs-comment">// unique_ptr只有‘移动赋值重载函数‘，参数是&amp;&amp; ，只能接右值，因此必须用std::move转换类型</span><br><br>std::unique_ptr&lt;A&gt; ptr_b = ptr_a; <span class="hljs-comment">// 编译不通过</span><br></code></pre></td></tr></table></figure>

<p><strong>std::move本身只做类型转换，对性能无影响。</strong> <strong>我们可以在自己的类中实现移动语义，避免深拷贝，充分利用右值引用和std::move的语言特性。</strong></p>
<h1 id="4-完美转发-std-forward"><a href="#4-完美转发-std-forward" class="headerlink" title="4. 完美转发 std::forward"></a><strong>4. 完美转发 std::forward</strong></h1><p>和<code>std::move</code>一样，它的兄弟<code>std::forward</code>也充满了迷惑性，虽然名字含义是转发，但他并不会做转发，同样也是做类型转换.</p>
<p>与move相比，forward更强大，move只能转出来右值，forward都可以。</p>
<blockquote>
<p>std::forward<T>(u)有两个参数：T与 u。 a. 当T为左值引用类型时，u将被转换为T类型的左值； b. 否则u将被转换为T类型右值。</p>
</blockquote>
<p>举个例子，有main，A，B三个函数，调用关系为：<code>main-&gt;A-&gt;B</code>，建议先看懂<em>2.3节对左右值引用本身是左值还是右值的讨论</em>再看这里：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">B</span><span class="hljs-params">(<span class="hljs-keyword">int</span>&amp;&amp; ref_r)</span> </span>&#123;<br>    ref_r = <span class="hljs-number">1</span>;<br>&#125;<br> <br><span class="hljs-comment">// A、B的入参是右值引用</span><br><span class="hljs-comment">// 有名字的右值引用是左值，因此ref_r是左值</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">A</span><span class="hljs-params">(<span class="hljs-keyword">int</span>&amp;&amp; ref_r)</span> </span>&#123;<br>    <span class="hljs-built_in">B</span>(ref_r);  <span class="hljs-comment">// 错误，B的入参是右值引用，需要接右值，ref_r是左值，编译失败</span><br>     <br>    <span class="hljs-built_in">B</span>(std::<span class="hljs-built_in">move</span>(ref_r)); <span class="hljs-comment">// ok，std::move把左值转为右值，编译通过</span><br>    <span class="hljs-built_in">B</span>(std::forward&lt;<span class="hljs-keyword">int</span>&gt;(ref_r));  <span class="hljs-comment">// ok，std::forward的T是int类型，属于条件b，因此会把ref_r转为右值</span><br>&#125;<br> <br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;<br>    <span class="hljs-built_in">A</span>(std::<span class="hljs-built_in">move</span>(a));<br>&#125;<br></code></pre></td></tr></table></figure>

<p>例2：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">change2</span><span class="hljs-params">(<span class="hljs-keyword">int</span>&amp;&amp; ref_r)</span> </span>&#123;<br>    ref_r = <span class="hljs-number">1</span>;<br>&#125;<br> <br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">change3</span><span class="hljs-params">(<span class="hljs-keyword">int</span>&amp; ref_l)</span> </span>&#123;<br>    ref_l = <span class="hljs-number">1</span>;<br>&#125;<br> <br><span class="hljs-comment">// change的入参是右值引用</span><br><span class="hljs-comment">// 有名字的右值引用是 左值，因此ref_r是左值</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">change</span><span class="hljs-params">(<span class="hljs-keyword">int</span>&amp;&amp; ref_r)</span> </span>&#123;<br>    <span class="hljs-built_in">change2</span>(ref_r);  <span class="hljs-comment">// 错误，change2的入参是右值引用，需要接右值，ref_r是左值，编译失败</span><br>     <br>    <span class="hljs-built_in">change2</span>(std::<span class="hljs-built_in">move</span>(ref_r)); <span class="hljs-comment">// ok，std::move把左值转为右值，编译通过</span><br>    <span class="hljs-built_in">change2</span>(std::forward&lt;<span class="hljs-keyword">int</span> &amp;&amp;&gt;(ref_r));  <span class="hljs-comment">// ok，std::forward的T是右值引用类型(int &amp;&amp;)，符合条件b，因此u(ref_r)会被转换为右值，编译通过</span><br>     <br>    <span class="hljs-built_in">change3</span>(ref_r); <span class="hljs-comment">// ok，change3的入参是左值引用，需要接左值，ref_r是左值，编译通过</span><br>    <span class="hljs-built_in">change3</span>(std::forward&lt;<span class="hljs-keyword">int</span> &amp;&gt;(ref_r)); <span class="hljs-comment">// ok，std::forward的T是左值引用类型(int &amp;)，符合条件a，因此u(ref_r)会被转换为左值，编译通过</span><br>    <span class="hljs-comment">// 可见，forward可以把值转换为左值或者右值</span><br>&#125;<br> <br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;<br>    <span class="hljs-built_in">change</span>(std::<span class="hljs-built_in">move</span>(a));<br>&#125;<br></code></pre></td></tr></table></figure>

<p>上边的示例在日常编程中基本不会用到，<code>std::forward</code>最主要运于模版编程的参数转发中，想深入了解需要学习<code>万能引用(T &amp;&amp;)</code>和<code>引用折叠(eg:&amp; &amp;&amp; → ?)</code>等知识</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/">软件开发</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/c/">c++</a>
                    
                      <a class="hover-with-bg" href="/tags/c11/">c11</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/11/13/04DevOps/28ubuntu%E7%8E%AF%E5%A2%83vim+copilot%E7%9A%84%E9%83%A8%E7%BD%B2%E4%B8%8E%E4%BD%BF%E7%94%A8/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">ubuntu环境下vim+copilot的部署与使用</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/10/12/04DevOps/27openssl%E7%9B%B8%E5%85%B3%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E6%B1%87%E6%80%BB/">
                        <span class="hidden-mobile">openssl相关使用方法汇总</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
